﻿using MyNes.Core;
using System;
using System.Collections.Generic;
using System.Text;

namespace BITUI.nes
{
    static class SimTool
    {
        public static byte[] pixel;
        public static int[] palette;

        static ushort buffersize = 1024 * 5;
        static byte[] audiobuf = new byte[buffersize * 10];
        static int audiowritepos = 0;
        static int audioreadpos = 0;
        static int samples_added = 0;
        public static void Init()
        {
            MyNesMain.Light_Init();
            //MyNes.Core.NesEmu.Light_SetupAudioSamples((ref short[] buffer, ref int samples_count) =>
            //{
            //    var buflen = audiobuf.Length;
            //    for (var i = 0; i < samples_count; i++)
            //    {
            //        var sample = (short)(buffer[i] * 0.5f);
            //        //var sb = BitConverter.GetBytes(sample);
            //        var hb = (byte)(sample >> 8); ;
            //        var lb = (byte)(sample & 0xFF);
            //        //明明说是小頭順序，这里sample 的 高位 为什么放在前面？
            //        //这不是和系统顺序反了吗
            //        if (BitConverter.IsLittleEndian)
            //        {
            //            audiobuf[audiowritepos++ % buflen] = hb;
            //            audiobuf[audiowritepos++ % buflen] = lb;
            //        }
            //        else
            //        {
            //            audiobuf[audiowritepos++ % buflen] = lb;
            //            audiobuf[audiowritepos++ % buflen] = hb;
            //        }
            //        samples_added++;
            //    }
            //});
            NesEmu.Light_SetupVideoRender((pixel, palette) =>
            {
                SimTool.pixel = pixel;
                SimTool.palette = palette;
            });
            loadedNesFile = null;
            conP0 = new Controller();
        }
        public unsafe static void FillAudioInt16(byte* outdata,int outlen)
        {
            if (samples_added < 3000) return;

            var buflen = audiobuf.Length;
            for (var i = 0; i < outlen; i += 2)
            {
                if (samples_added > 0)
                {
                    outdata[i] = audiobuf[audioreadpos++ % buflen];
                    outdata[i + 1] = audiobuf[audioreadpos++ % buflen];
                    samples_added--;
                }
            }
        }
        public static string loadedNesFile
        {
            get;
            private set;
        }
        public static Controller conP0
        {
            get;
            private set;
        }
        public static bool LoadAndStartGame(string nesfilename)
        {
            if (loadedNesFile != null)
            {
                throw new Exception("have a game,close first.");
            }
            loadedNesFile = nesfilename;
            MyNes.Core.NesEmu.LoadGame(nesfilename, out bool success, true);
            MyNes.Core.NesEmu.SetupControllersP1(conP0);

            return success;
        }
        static void Close()
        {
            if (loadedNesFile != null)
            {
                try
                {
                    MyNes.Core.MyNesMain.Shutdown();
                }
                catch
                {

                }
            }
            loadedNesFile = null;
        }
    }
}
